<#assign pojo=oneToManyPojo.getOne()>
<#assign many=oneToManyPojo.getMany()>
<#assign ones=oneToManyPojo.getOnes()>
<#assign fields=pojo.getFilterdPojoFields()>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>
<#assign declarationNameFirstLetterLower = pojo.importType(pojo.getDeclarationNameFirstLetterLower())>
<#assign realPackageAndClassPath = "${pojo.getRealPackageAndClassPath()}">
<#assign masterid = pojo.getDeclarationNameFirstLetterLower()+"Id">
import {Component, OnInit, ViewChild} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {${declarationName}GridComponent} from '../../components/${declarationNameFirstLetterLower}-grid/${declarationNameFirstLetterLower}-grid.component';
import {${declarationName}FilterComponent} from '../../components/${declarationNameFirstLetterLower}-filter/${declarationNameFirstLetterLower}-filter.component';
import {${declarationName}FormComponent} from '../../components/${declarationNameFirstLetterLower}-form/${declarationNameFirstLetterLower}-form.component';
import {${declarationName}Service} from '../../services/${declarationNameFirstLetterLower}.service';

@Component({
  selector: 'app-${declarationNameFirstLetterLower}-index',
  templateUrl: './${declarationNameFirstLetterLower}-index.component.html',
  styles: []
})

export class ${declarationName}IndexComponent implements OnInit {

  @ViewChild('${declarationNameFirstLetterLower}Filter') ${declarationNameFirstLetterLower}Filter: ${declarationName}FilterComponent;
  @ViewChild('${declarationNameFirstLetterLower}Grid') ${declarationNameFirstLetterLower}Grid: ${declarationName}GridComponent;
  @ViewChild('${declarationNameFirstLetterLower}Form') ${declarationNameFirstLetterLower}Form: ${declarationName}FormComponent;
  currentStep: string;

  currentSelection: any;

  constructor(private fb: FormBuilder, private ${declarationNameFirstLetterLower}Service: ${declarationName}Service) {

  }

  ngOnInit() {
    this.currentStep = 'list';
  }

  ngAfterViewInit() {
    this.onSearch();
  }

  onSearch(): void {
    this.${declarationNameFirstLetterLower}Grid.setParams(this.${declarationNameFirstLetterLower}Filter.getParams());
    this.${declarationNameFirstLetterLower}Grid.flushData();
  }

  onEdit(entity) {
    this.currentStep = 'form';
    this.currentSelection = entity;
  }

  onAdd() {
    this.currentStep = 'form';
    this.currentSelection = {};
  }

  onRemove(entity) {
    this.currentStep = 'form';
    this.currentSelection = entity;
  }

  onProject(entity): void {

  }

  onEditSelect() {
    const selections = this.${declarationNameFirstLetterLower}Grid.getSelections();
    if (selections.length === 1) {
      this.onEdit(selections[0]);
    }
  }

  async onRemoveSelections() {
    const selections = this.${declarationNameFirstLetterLower}Grid.getSelections();
    const ids = [];
    selections.forEach(function (item) {
      ids.push(item.id);
    });
    await this.${declarationNameFirstLetterLower}Service.deleteProgrammes(ids);
    this.${declarationNameFirstLetterLower}Grid.flushData();
  }

  async onOk(entity) {
    this.currentStep = 'list';
    Object.assign(this.currentSelection, entity);
    let result;
    if (this.currentSelection.id) {
      result = await this.${declarationNameFirstLetterLower}Service.updateProgramme(this.currentSelection);
    } else {
      result = await this.${declarationNameFirstLetterLower}Service.saveProgramme(this.currentSelection);
    }
    this.${declarationNameFirstLetterLower}Grid.flushData();
  }

  onCancel(entity) {
    this.currentStep = 'list';
  }
}
